﻿using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace KBEngine.Setting
{
    [XmlRoot("root")]
    public class Entities : IXmlSerializable
    {
        public List<Info> entityInfos = new List<Info>();

        public class Info
        {
            /// <summary>
            /// 实体名称
            /// </summary>
            public string entityName = string.Empty;

            /// <summary>
            /// 是否存在客户端实现
            /// </summary>
            public bool hasClient = false;
        }

        #region XmlSerializable
        public XmlSchema GetSchema() => null;
        public void ReadXml(XmlReader reader)
        {
            entityInfos.Clear();
            while (reader.Read())
            {
                if (reader.NodeType != XmlNodeType.Element)
                    continue;

                Info info = new Info();
                info.entityName = reader.Name;
                var hasClientStr = reader.GetAttribute("hasClient");
                if (!string.IsNullOrEmpty(hasClientStr))
                    info.hasClient = bool.Parse(hasClientStr);
                entityInfos.Add(info);
            }
        }
        public void WriteXml(XmlWriter writer)
        {
            foreach (var item in entityInfos)
            {
                writer.WriteStartElement(item.entityName);
                if (item.hasClient)
                    writer.WriteAttributeString("hasClient", "true");
                writer.WriteEndElement();
            }
        }
        #endregion
    }
}
